package com.easyfeeling.test;

import com.easyfeeling.test.entity.Provinces;
import com.easyfeeling.test.service.ProvincesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(value = "省份信息表", description = "省份信息表相关api")
@RestController
@RequestMapping(value = "/provinces")
public class ProvincesController {

    private static final Logger logger = LoggerFactory.getLogger(ProvincesController.class);

	@Autowired
	private ProvincesService provincesService;

    @RequestMapping(value = "/detail/{provinceid}", method = RequestMethod.GET)
    public Provinces detail(@RequestParam("provinceid") String provinceid){
        try {
            return provincesService.detail(provinceid);
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public int add(HttpServletRequest request,
                   @ApiParam(value = "省份信息", required = true) @RequestBody Provinces bean){
        try {
            provincesService.add(bean);
            return 1;
        } catch (Exception e) {
            logger.error("新增地市失败",e);
            return 0;
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public int update(@ApiParam(value = "省份id", required = true) @PathVariable("id") Integer id,
                      @ApiParam(value = "省份信息", required = true) @RequestBody Provinces bean){
        bean.setId(id);
        try {
            provincesService.update(bean);
            return 1;
        } catch (Exception e) {
            logger.error("更新地市信息失败",e);
            return 0;
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public int delete(@RequestParam("provinceid") String provinceid){
        try {
            provincesService.delete(provinceid);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

}